﻿<!DOCTYPE HTML>
<html>
    <head>
        <meta charset="utf-8" name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0"/>
        <title>Clipboard</title>
        <script src="jquery.min.js"></script>
        <script src="/jquery.textareafullscreen/jquery.textareafullscreen.js"></script>
        <script src='jquery.base64.js'></script>
        <link rel="stylesheet" href="/jquery.textareafullscreen/textareafullscreen.css">
        <script type="text/javascript">
          $(document).ready(function() {
            $('#editView').textareafullscreen({
              // overlay: true, // Overlay
              maxWidth: '95%', // Max width
              maxHeight: '95%', // Max height
              autoExpand: true, // Auto Max
              // key: 'f' // default null, crtl + key to toggle fullscreen
            });

            $.base64.utf8encode = true;
            
            var isWait = false

            //取参数
            var getQueryStringByName = function(name){
              var result = location.search.match(new RegExp("[\?\&]" + name+ "=([^\&]+)","i"));
              if(result == null || result.length < 1){
                return "";
              }
              return result[1];
            }
            
            //sync
            var funcSync = function(str){
                str = $.base64.btoa(str, true)
                str = encodeURIComponent(str)
                console.log(str)
                $.ajax({
                  async : true,
                  url : "http://x-86.com:8001/clipboard?act=write&content="+str+"&file="+getQueryStringByName("tag"),
                  type : "GET",
                  dataType : "jsonp",
                  jsonp: "callback",
                  success: function(response, status, xhr){
                    console.log(response);
                  },
                  error: function (err) {
                      console.log(err);
                  }
              });

            }
            // 响应
            $('#editView').bind('input propertychange', function() {
              
              if(isWait==true){

              }else{
                isWait = true
                setTimeout(() => {
                  var str = $.trim($('#editView').val());
                  funcSync(str)
                  isWait = false
                }, 2000)
              }

            });

              $.ajax({
                async : true,
                url : "http://x-86.com:8001/clipboard?act=read&file="+getQueryStringByName("tag"),
                type : "GET",
                dataType : "jsonp",
                jsonp: "callback",
                success: function(response, status, xhr){
                    var data = decodeURIComponent(response.content)
                    console.log(data)
                  data = $.base64.atob(data, true)
                  $("#editView").val(data)
                },
                error: function (err) {
                    console.log(err);
                }
            });

            console.log(getQueryStringByName("tag"))
          });
      </script>

    </head>
    <body>
      <h1 align="center">Clipboard</h1>
      <div class="container">
        <textarea id="editView" style="font-family:helvetica;font-size:24px;color:gray"></textarea>
      </div>
    </body>
</html>